<template>
  <div class="login-box">
    <div class="login-wrapper">
      <img src="../../assets/images/login-left.jpg" alt="">
      <div class="content-wrapper">
        <div class="title">班级管理系统</div>
        <div class="form-wrapper">
          <el-input
            v-model="username"
            placeholder="请输入账号"
            :prefix-icon="User"
          />
          <el-input
            v-model="password"
            placeholder="请输入密码"
            type="password"
            :prefix-icon="Lock"
            show-password
          />
          <el-button type="primary" @click="loginHandler" :loading="loading">登 录</el-button>
        </div>
        <div class="register-wrapper">
          <span>还没账号？</span><span @click="$router.push('/register')">去注册</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { login } from '@/api';
import storage from '@/utils/storage';
// import { useUserStore } from '@/store';
import { Lock, User } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { ref } from 'vue';
import { useRouter } from 'vue-router';

// const userStore = useUserStore()
const router = useRouter()
const username = ref('')
const password = ref('')
const loading = ref(false)

const loginHandler = () => {
  if (!username.value || !password.value) {
    ElMessage.error('请输入账号密码')
    return
  }
  loading.value = true
  // axios.post('http://152.136.202.150:3000/api/auth/login', {
  //   username: username.value,
  //   password: password.value
  // }).then(res => {
  //   console.log(res);
  //   if (res.status === 200) {
  //     storage.set('token', res.data.token)
  //   }
  //   loading.value = false
  // })
  login({
    username: username.value,
    password: password.value
  }).then(res => {
    storage.set('token', res.token)
    storage.set('menus', res.menus)
    storage.set('userInfo', res.user)
    loading.value = false
    router.push('/main/home')
  }).catch(() => {
    loading.value = false
  })
  // userStore.loginAction({
  //   username: username.value,
  //   password: password.value
  // }, (flag) => {
  //   loading.value = false
  //   if (flag) {
  //     router.push('/home')
  //   }
  // })
}

</script>

<style lang="scss" scoped>
// background-color：背景颜色（如 #fff、rgba(0,0,0,0.5)）
// background-image：背景图片（如 url(...)）
// background-repeat：图片重复方式（repeat/no-repeat/repeat-x/repeat-y）
// background-position：图片位置（如 center、top left、50% 50%）
// background-size：图片大小（如 cover、contain、100% 100%）
// background-attachment：滚动行为（scroll/fixed/local）
// background-origin/background-clip：背景定位 / 绘制区域（较少用）
.login-box {
  width: 100vw;
  height: 100vh;
  background: url('../../assets/images/login-bg.png') no-repeat center center / cover fixed;
  // background-image: url('../assets/images/login-bg.png');
  // background-repeat: no-repeat;
  // background-position: center center;
  // background-size: cover;
  // background-attachment: fixed;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-wrapper {
    width: 800px;
    height: 400px;
    display: flex;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    &:hover {
      box-shadow: 0 20px 25px -5px rgba(64, 158, 255, 0.3);
      transform: scale(1.01);
    }
    img {
      width: 400px;
      height: 400px;
      object-fit: cover;
    }
    .content-wrapper {
      flex: 1;
      background: #fafafa;
      padding: 16px;
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      .title {
        font-size: 24px;
        font-weight: bold;
        color: rgb(64, 158, 255);
        text-align: center;
      }
      .form-wrapper {
        display: flex;
        flex-direction: column;
        justify-content: space-evenly;
        height: 50%;
      }
      .register-wrapper{
        display: flex;
        justify-content: center;
        align-items: center;
        span {
          font-size: 12px;
          color: #409EFF;
          &:first-child {
            color: #666666;
          }
          &:last-child {
            cursor: pointer;
          }
        }  
      }
    } 
  }
}
</style>